﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerKhauPhanAnChiTietDao : IKhauPhanAnChiTietDao
    {
        public SqlServerKhauPhanAnChiTietDao()
        {
            SQLUtility.TableName = "KhauPhanAnChiTiet";
        }

        //Trả về đối tượng báo ăn
        private KhauPhanAnChiTiet MakeKhauPhanAnChiTiet(DataRow row)
        {
            Guid _id = Guid.Parse(row["Id"].ToString());
            Guid _khauPhanAnId = Guid.Parse(row["KhauPhanAnId"].ToString());
            Guid _thucPhamId = Guid.Parse(row["ThucPhamId"].ToString());
            double _soLuong = double.Parse(row["SoLuong"].ToString());
            double _tien = double.Parse(row["Tien"].ToString());
         return new KhauPhanAnChiTiet(_id, _khauPhanAnId, _thucPhamId, _soLuong, _tien);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<KhauPhanAnChiTiet> MakeKhauPhanAnChiTiets(DataTable dt)  
        {
            IList<KhauPhanAnChiTiet> list = new List<KhauPhanAnChiTiet>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeKhauPhanAnChiTiet(row));
            }
            return list;
        }
        private DbParameter[] Take(KhauPhanAnChiTiet KhauPhanAnChiTiet)
        {
            DbParameter[] parameters = new DbParameter[5];
            parameters[0] = new SqlParameter("@Id", KhauPhanAnChiTiet.Id);
            parameters[1] = new SqlParameter("@KhauPhanAnId", KhauPhanAnChiTiet.KhauPhanAnId);
            parameters[2] = new SqlParameter("@ThucPhamId", KhauPhanAnChiTiet.ThucPhamId);
            parameters[3] = new SqlParameter("@SoLuong", KhauPhanAnChiTiet.SoLuong);
            parameters[4] = new SqlParameter("@Tien", KhauPhanAnChiTiet.Tien);
            return parameters;
        }

        public IList<Business_Objects.KhauPhanAnChiTiet> GetKhauPhanAnChiTiet()
        {
            SQLUtility.TableName = "KhauPhanAnChiTiet";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeKhauPhanAnChiTiets(dt); 
        }

        public Business_Objects.KhauPhanAnChiTiet GetKhauPhanAnChiTietByID(string KhauPhanAnChiTietID)
        {
            SQLUtility.TableName = "KhauPhanAnChiTiet";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", KhauPhanAnChiTietID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeKhauPhanAnChiTiet(row);
            }
        }

        public void InsertKhauPhanAnChiTiet(Business_Objects.KhauPhanAnChiTiet KhauPhanAnChiTiet)
        {
            SQLUtility.TableName = "KhauPhanAnChiTiet";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(KhauPhanAnChiTiet), true);
        }

        public int UpdateKhauPhanAnChiTiet(Business_Objects.KhauPhanAnChiTiet KhauPhanAnChiTiet)
        {
            SQLUtility.TableName = "KhauPhanAnChiTiet";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(KhauPhanAnChiTiet)); }
            catch { return -1; }
        }

        public int DeleteKhauPhanAnChiTiet(Business_Objects.KhauPhanAnChiTiet KhauPhanAnChiTiet)
        {
            SQLUtility.TableName = "KhauPhanAnChiTiet";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(KhauPhanAnChiTiet)); }
            catch { return -1; }
        }
    }
}
